﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using log4net;
using System.Data;
using QualityRpt.Service;
using QualityRpt.Utils;
using QualityRpt.Dao;

namespace QualityRPT.include
{
    public partial class api : System.Web.UI.Page
    {
        static ILog logger = LogManager.GetLogger("api");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                string action = Request.Params["action"];
                Response.Clear();
                Response.Charset = "UTF-8";
                DateTime now = DateTime.Now;
                DateTime utcnow = now.ToUniversalTime();
                StringBuilder rs = new StringBuilder();
                rs.Append("{\"site\":\"ETCQTrack\"");
                switch (action)
                {
                    #region 多选列表
                    case "getmulsclist"://获得型号列表
                        try
                        {
                            string strspin = Request.Params["spin"];
                            string strdev = Request.Params["dev"];
                            if (strdev == null || strdev.Trim() == string.Empty)
                            {
                                strdev = "0";
                            }
                            if (strspin != null && strspin.Trim() != string.Empty && strspin.Trim() != "null")
                            {
                                DataTable scTable = DeviceSpecService.GetBySpIn(strspin, Convert.ToInt32(strdev));
                                rs.Append(",\"responseStatus\":200");
                                rs.Append(",\"data\":[");
                                for (int i = 0; i < scTable.Rows.Count; i++)
                                {
                                    DataRow row = scTable.Rows[i];
                                    if (i > 0)
                                    {
                                        rs.Append(",");
                                    }
                                    rs.Append("{\"id\":" + Convert.ToInt32(row["ID"]));
                                    rs.Append(",\"text\":\"" + row["SPEC_NAME"].ToString() + "\"}");
                                }
                                rs.Append("]");
                            }
                            else
                            {
                                rs.Append(",\"responseStatus\":401");
                            }
                        }
                        catch (Exception ex)
                        {
                            rs.Append(",\"responseStatus\":500");
                            logger.Error(ex);
                        }
                        break;
                    case "getmulcnlist"://获得合同列表
                        try
                        {
                            string strscin = Request.Params["scin"];
                            string strdev = Request.Params["dev"];
                            if (strdev == null || strdev.Trim() == string.Empty)
                            {
                                strdev = "0";
                            }
                            if (strscin != null && strscin.Trim() != string.Empty && strscin.Trim()!="null")
                            {
                                DataTable cnTable = DeviceContractDetailService.GetBySpecIn(strscin,Convert.ToInt32(strdev),null);
                                rs.Append(",\"responseStatus\":200");
                                rs.Append(",\"data\":[");
                                for (int i = 0; i < cnTable.Rows.Count; i++)
                                {
                                    DataRow row = cnTable.Rows[i];
                                    if (i > 0)
                                    {
                                        rs.Append(",");
                                    }
                                    rs.Append("{\"id\":\"" + row["CONTRACT_NO"].ToString() + "\"");
                                    rs.Append(",\"text\":\"" + row["CONTRACT_NO"].ToString() + "\"}");
                                }
                                rs.Append("]");
                            }
                            else
                            {
                                rs.Append(",\"responseStatus\":401");
                            }
                        }
                        catch (Exception ex)
                        {
                            rs.Append(",\"responseStatus\":500");
                            logger.Error(ex);
                        }
                        break;
				case "getmulcnlistBySp"://获得合同列表,通过厂商ids
					try
					{
						string strspin = Request.Params["spin"];
						string strdev = Request.Params["dev"];
						if (strdev == null || strdev.Trim() == string.Empty)
						{
							strdev = "0";
						}
						if (strspin != null && strspin.Trim() != string.Empty && strspin.Trim()!="null")
						{
							DataTable cnTable = DeviceContractDetailService.GetListBySpIn(Convert.ToInt32(strdev),strspin,null);
							rs.Append(",\"responseStatus\":200");
							rs.Append(",\"data\":[");
							for (int i = 0; i < cnTable.Rows.Count; i++)
							{
								DataRow row = cnTable.Rows[i];
								if (i > 0)
								{
									rs.Append(",");
								}
								rs.Append("{\"id\":\"" + row["CONTRACT_NO"].ToString() + "\"");
								rs.Append(",\"text\":\"" + row["CONTRACT_NO"].ToString() + "\"}");
							}
							rs.Append("]");
						}
						else
						{
							rs.Append(",\"responseStatus\":401");
						}
					}
					catch (Exception ex)
					{
						rs.Append(",\"responseStatus\":500");
						logger.Error(ex);
					}
					break;
                    case "getmulplazalist"://获得路段下的广场列表
                        try
                        {
                            string strrdin = Request.Params["rdin"];
                            if (strrdin != null && strrdin.Trim() != string.Empty)
                            {
                                DataTable plTable = NetWorkPlazaService.GetListByRdIn(strrdin);
                                rs.Append(",\"responseStatus\":200");
                                rs.Append(",\"data\":[");
                                for (int i = 0; i < plTable.Rows.Count; i++)
                                {
                                    DataRow row = plTable.Rows[i];
                                    if (i > 0)
                                    {
                                        rs.Append(",");
                                    }
                                    rs.Append("{\"id\":" + Convert.ToInt32(row["ORGAN_ID"]));
                                    rs.Append(",\"text\":\"" + row["ORGAN_NAME"].ToString() + "\"}");
                                }
                                rs.Append("]");
                            }
                            else
                            {
                                rs.Append(",\"responseStatus\":401");
                            }
                        }
                        catch (Exception ex)
                        {
                            rs.Append(",\"responseStatus\":500");
                            logger.Error(ex);
                        }
                        break;
                    #endregion
                    case "getobubycn"://根据合同号获得设备合同信息
                        try
                        {
                            string strcnin = Request.Params["cnno"];
                            if (strcnin != null && strcnin.Trim() != string.Empty)
                            {
                                DataTable detailtTable = DeviceContractDetailService.GetListByCnIn(DeviceService.OBU_DEV, strcnin,null);
                                if (detailtTable.Rows.Count > 0)
                                {
                                    rs.Append(",\"responseStatus\":200");
                                }
                            }
                            else
                            {
                                rs.Append(",\"responseStatus\":401");
                            }
                        }
                        catch (Exception ex)
                        {
                            rs.Append(",\"responseStatus\":500");
                            logger.Error(ex);
                        }
                        break;
                    case "removecache":
                        try
                        {
                            string cacheprefix = Request.Params["pref"];
                            if (cacheprefix != null && cacheprefix.Trim() != string.Empty)
                            {
                                if (cacheprefix == "0")
                                {
                                    CacheManager.RemoveAll();
                                }
                                else
                                {
                                    CacheManager.RemoveByPrefix(cacheprefix);
                                }
                                rs.Append(",\"responseStatus\":200");
                            }
                            else
                            {
                                rs.Append(",\"responseStatus\":401");
                            }
                        }
                        catch (Exception ex)
                        {
                            rs.Append(",\"responseStatus\":500");
                            logger.Error(ex);
                        }
                        break;
                }
                rs.Append("}");
                Response.Write(rs.ToString());
            }
        }
    }
}